﻿using System.IO;
using FileSynchronizer.DataAccess;
using FileSynchronizer.Forms;

namespace FileSynchronizer.Objects
{
    //---------------------------------------------------------------------------------------------------------------------------------------
    // Code based upon the State Pattern, as described by the GOF
    //
    // http://en.wikipedia.org/wiki/State_pattern and http://en.wikipedia.org/wiki/Talk:State_pattern#Disputed
    //
    // This is a Concrete State Object
    //
    //---------------------------------------------------------------------------------------------------------------------------------------

    class ProgramState_SyncFolderPair : ProgramState
    {
        #region Constructors
        //---------------------------------------------------------------------------------------------------------------------------------------
        public ProgramState_SyncFolderPair(ProgramContext TheProgramContext)
            : base(TheProgramContext)
        {
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Public Methods
        //---------------------------------------------------------------------------------------------------------------------------------------
        // Called when we want to Synchronize all FolderPairs
        //---------------------------------------------------------------------------------------------------------------------------------------
        public override bool SynchronizeFolderPair(FolderPair TheFolderPair)
        {
            Form_Progress frm_SyncProgress;
            ProgramState_Idle NextState;

            bool OperationResult = true;

            if (Directory.Exists(TheFolderPair.LeftFolder) & Directory.Exists(TheFolderPair.RightFolder))
            {
                frm_SyncProgress = new Form_Progress(TheFolderPair);
                frm_SyncProgress.ShowDialog();
                if (!frm_SyncProgress.SyncSuccessfull)
                {
                    Program.TheErrorForm.AddErrorString("Failed to synchronize " + TheFolderPair.LeftFolder + " with " + TheFolderPair.RightFolder);
                }
                else
                {
                    OperationResult = false;
                }
                frm_SyncProgress = null;

                NextState = new ProgramState_Idle(CurrentProgramContext);
                ChangeState(NextState);
                CurrentProgramContext.CurrentState = State.Idle;
            }
            return OperationResult;
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion
    }
}
